package com.F05.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import com.F05.actionform.F0501ActionForm;
import com.dao.DataOperations;
import com.dbtools.Dispose;
import com.exception.SelectException;
import com.util.CommonTools;

public class F0501DAO extends DataOperations{
	Logger log = Logger.getLogger(F0501DAO.class);
	public F0501DAO(){
		
	}
    public F0501DAO(Connection connection){
        super(connection);
    }
    
    public List queryList(F0501ActionForm actionForm) throws SelectException{
    	List list = new ArrayList();
    	StringBuffer sql = new StringBuffer();
    	sql.append(" select id,euro_rmb,usd_rmb,euro_usd,exchange_date ");
        sql.append(" from f_currencies ");
        sql.append(" where 1=1 ");
 
    	if(!actionForm.getExchangeDate1().equals("")){
    		sql.append(" and exchange_date >= convert(datetime,'"+actionForm.getExchangeDate1()+"')");
    	}
    	if(!actionForm.getExchangeDate2().equals("")){
    		sql.append(" and exchange_date <= convert(datetime,'"+actionForm.getExchangeDate2()+"')");
    	}

    	ResultSet rest = null;
    	try{
    		log.info(sql.toString());
    		rest = this.executeQuery(sql.toString());
    		while(rest.next()){
    			Map map = new HashMap();
    			map.put("ID", CommonTools.null2String(rest.getString("id")));
    			map.put("euroRmb", CommonTools.null2String(rest.getString("euro_rmb")));
    			map.put("usdRmb", CommonTools.null2String(rest.getString("usd_rmb")));
				map.put("euroUsd", CommonTools.null2String(rest.getString("euro_usd")));
				map.put("exchangeDate", CommonTools.getDateTime(rest.getDate("exchange_date")));
			
				list.add(map);
    		}
    	}catch(SQLException e){
    		e.printStackTrace();
    	}finally{
			Dispose.dispose(rest);
		}
    	return list;
    }
    
    public boolean delete(String id,F0501ActionForm form){
		boolean flag=true;
		StringBuffer sql=new StringBuffer();
		List sqlList = new ArrayList();
		sql.append("DELETE FROM f_currencies  ");
		sql.append("WHERE id ="+id);
		sqlList.add(sql.toString());
		try{
			log.info("sql is "+sql.toString());
			flag=super.executeUpdate(sqlList,true);
			form.setErrorFlag("0");
		}catch(Exception e){
			e.printStackTrace();
			form.setErrorFlag("1");
		}
		return flag;
	}
    
    public boolean insert(F0501ActionForm form){
		boolean flag=false;
		StringBuffer sql=new StringBuffer();
		List sqlList = new ArrayList();
		try{
			sql.append("INSERT INTO f_currencies  ");
			sql.append("  (euro_rmb,usd_rmb,euro_usd,exchange_date ) ");
			sql.append("VALUES ('"+form.getEuroRmb()+"', '"+form.getUsdRmb()+"','"+form.getEuroUsd()+"', convert(datetime,'"+form.getExchangeDate()+"'))");
			
			sqlList.add(sql.toString());
			log.info("sql is "+sql.toString());
			flag=super.executeUpdate(sqlList,true); 
			form.setErrorFlag("0");
		}catch(SQLException ex){
			if("1".equals(String.valueOf(ex.getErrorCode()))){
				//form.setErrorFlag("主键重复,错误序号: "+String.valueOf(ex.getErrorCode()));
				form.setErrorFlag("3");
			}else{
				form.setErrorFlag("1");
			}
		}catch(Exception e){
			e.printStackTrace();
			form.setErrorFlag("1");
		}
		return flag;
	}
    
    public List selectById(String id) throws SelectException{
    	List list = new ArrayList();
    	StringBuffer sql = new StringBuffer();    	
    	
    	sql.append("SELECT id,euro_rmb,usd_rmb,euro_usd,exchange_date  FROM f_currencies ");
    	sql.append(" where id = ").append(id);
    	ResultSet rest = null;
    	try {
    		log.info("sql is "+sql);
    		rest = this.executeQuery(sql.toString());
			while(rest.next()){
				Map map = new HashMap();
				map.put("ID", CommonTools.null2String(rest.getString("id")));
    			map.put("euro_rmb", CommonTools.null2String(rest.getString("euro_rmb")));
    			map.put("usd_rmb", CommonTools.null2String(rest.getString("usd_rmb")));
				map.put("euro_usd", CommonTools.null2String(rest.getString("euro_usd")));
				map.put("exchange_date", CommonTools.getDateTime(rest.getDate("exchange_date")));
				
				list.add(map);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			Dispose.dispose(rest);
		}
		return list;
    }
    
    public boolean update(F0501ActionForm form){
    	boolean flag =false;
    	StringBuffer sql=new StringBuffer();
		List sqlList = new ArrayList();
		sql.append("update f_currencies set ");
		sql.append(" euro_rmb='"+form.getEuroRmb()+"',");
		sql.append(" usd_rmb='"+form.getUsdRmb()+"',");
		sql.append(" euro_usd='"+form.getEuroUsd()+"',");
		sql.append(" exchange_date=convert(datetime,'"+form.getExchangeDate()+"') ");
		sql.append(" where id="+form.getId());
		sqlList.add(sql.toString());
		
		try{
			log.info("sql is "+sql.toString());
			flag=super.executeUpdate(sqlList,true); 	
			form.setErrorFlag("0");
		}catch(Exception e){
	        e.printStackTrace();
	        form.setErrorFlag("1");
		}
		return flag;
    }
    
}
